﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using NLog;
using TDAPIOLELib;
using System.Windows.Forms;
using System.Threading;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Net.Security;
namespace QCCure
{
    class Program
    {
        public static string qcPath { get; set; }
        public static string qcTestSetName { get; set; }
        public static string qcTestName { get; set; }

        public static string testResultsFile { get; set; }
        public static char DELIMITER { get; set; }

        private static Logger log = NLog.LogManager.GetCurrentClassLogger();

        //public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        //{
        //  return true;
        //}

        [STAThread]
        static void Main(string[] args)
        {
            try
            {

                // ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

                MainForm form = new MainForm();

                Application.EnableVisualStyles();
                Application.Run(form);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show(ex.Message);
            }
        }

        /**
         * parse a csv file to get test results
         * 
         * the expected file format is:
         * qcTestName, qcTestStatus
         * 
         * e.g.:
         * TEST_ID_1, Passed
         * 
         * @param string filename
         * @return Dictionary<string, string> 
         */
        static Dictionary<string, string> getTestResultsFromFile(string filename)
        {
            log.Debug("reading file: " + filename);
            Dictionary<string, string> results = new Dictionary<string, string>();
            string[] lines = File.ReadAllLines(filename);

            if (!File.Exists(filename))
            {
                throw new IOException("file doesn't exist: " + filename);
            }

            int lineCount = 0;
            foreach (string line in lines)
            {
                lineCount++;
                log.Debug("line#" + lineCount + ": " + line);
                line.Trim();

                if (line.Length == 0)
                {
                    // skip blank lines
                    log.Debug("skipping blank line #" + lineCount);
                }

                else if (line[0] == '#')
                {
                    // skip comments
                    log.Debug("skipping comment line #" + lineCount);
                }
                else
                {
                    string[] result = line.Split(DELIMITER);

                    if (result.Length != 2)
                    {
                        // skip incorrectly formatted lines
                        log.Warn("skipping incorrectly formatted line " + lineCount);
                    }
                    else
                    {
                        string testName = result[0].Trim();
                        string testStatus = result[1].Trim();

                        log.Debug("testName: " + testName);
                        log.Debug("testStatus: " + testStatus);
                        
                        try
                        {
                            results.Add(testName, testStatus);
                        }
                        catch (ArgumentException e)
                        {
                            log.Error("Exception: " + e.Message + " on line #" + lineCount);
                        }
                    }
                }

            } // end foreach loop

            return results;
        }
    }
}
